import es
import cfglib

from wcs.libs.constants import *

"""
This file will simply be to create all global configurations for WCS. We keep
this separate to WCS as I don't like mixing configurations with generic
functionality so separating makes it easier to update and neater.
"""

class ConfigObject(cfglib.AddonCFG):
    """
    The only reason this object is here is to provide backwards compatibility
    with old style races. After WCS was released SuperDave release cfglib which
    had the same functionality as my mock up class but provided better
    execution. For this reason, I am deciding to scrap my idea and just use his
    library instead. To keep the BC, I need to create function wrapers to adhere
    to my function naming processes.
    """
    def __init__(self, raceName):
        """
        Constructor, set up the CFG path and remove any old files to clean up
        the folders.

        @param string raceName The name of the race this configuration is for.
        """
        raceFolder = RACE_CFG.joinpath(raceName)
        self.raceName = raceName
        self.pathDir = raceFolder.joinpath("config.cfg")
        if not raceFolder.exists():
            raceFolder.mkdir()
        cfglib.AddonCFG.__init__(self, str(self.pathDir))
        testPath = RACE_PATH.joinpath(raceName, "config.cfg")
        if testPath.exists():
            testPath.remove()

    def addComment(self, *args, **kw):
        """
        Adds a comment to the configuration file.
        """
        if not args:
            args = list(args)
            args.append("")
            args = tuple(args)
        self.text(*args, **kw)

    def addVar(self, *args, **kw):
        """
        Adds a variable to the configuration file

        @return es.ServerVar The server variable instance for the variable
        """
        return self.cvar(*args, **kw)

    def load(self, *args, **kw):
        """
        Writes and executes the configuration file
        """
        self.write()
        es.server.cmd('exec "wcs/races/%s/config.cfg"' % self.raceName)

cfgFile = WCS_CFG_PATH.joinpath("wcs_config.cfg")
config  = cfglib.AddonCFG(cfgFile)
config.text("%s Version %s" % (info.name, info.version))
config.text("Created by %s" % info.author)
config.text("Get more help at %s" % info.url)

config.text("", comment=False) # Add a spacer between header and cvars

config.text("#" * 50)
config.text(" General Configuration ".center(50, "#"))
config.text("#" * 50)
modPrefix = config.cvar("wcs_prefix", "WCS", "The prefix used in all messages" +
                                             " in WarCraft Source.")

config.text("", comment=False) # Add a spacer between header and cvars

config.text("#" * 50)
config.text(" XP Configurations ".center(50, "#"))
config.text("#" * 50)
startXP = config.cvar("wcs_start_xp", 300, "The amount of experience required" +
                                           " to gain level 1 in a race.")
xpIncrement = config.cvar("wcs_xp_increment", 75, "The amount each additional" +
                 " level increments the required experience to acquire levels.")
killXP = config.cvar("wcs_kill_xp", 35, "The xp gain for killing an enemy")
headshotXP = config.cvar("wcs_headshot_xp", 25, "The amount of additional" +
                            " experience gained from acquiring a headshot kill")
levelDiffXP = config.cvar("wcs_level_difference_xp", 4, "The amount of " +
        "experience gained multiplied by the level difference of your enemy")
winRoundXp = config.cvar("wcs_win_round_xp", 15, "The amount of experience " +
             "gained for each player on the winning team at the end of a round")
hostageXP = config.cvar("wcs_hostage_xp", 15, "Amount of experiece gained for" +
                      " each hostage resucued multiplied by the player's level")
bombDefuseXP = config.cvar("wcs_defuse_xp", 50, "Amount of eperience gained" +
                                                " for defusing a bomb")
bombPlantXP = config.cvar("wcs_plant_xp", 25, "Amount of experience gained " +
                                              " for planting a bomb")
bombExplodedXP = config.cvar("wcs_explode_xp", 25, "Amount of eperience given" +
                             " to each terrorist alive when the bomb explodes")


raceLoaderPath = WCS_CFG_PATH.joinpath("wcs_race_loader.cfg")
raceLoader = cfglib.AddonCFG(raceLoaderPath)
raceLoader.text("%s Version %s" % (info.name, info.version))
raceLoader.text("Created by %s" % info.author)
raceLoader.text("Get more help at %s" % info.url)
raceLoader.text("", comment=False) # Add a spacer between header and description
raceLoader.text("""\
// This file will be used to load all of the races. This is where you will
// enable / disable all races by default.

// To load a race, simply use the command:
// wcs load "<race name>"
// e.g: wcs load "Undead Scourge"

// To load all races within the ../addons/wcs/races/ folder use this command:
// wcs loadallraces

// Add all race loads below:
// -----------------------------------------------------------------------------
""", comment=False)

raceLoader.command("wcs")

raceLoader.text("-" * 77)

itemLoaderPath = WCS_CFG_PATH.joinpath("wcs_item_loader.cfg")
itemLoader = cfglib.AddonCFG(itemLoaderPath)
itemLoader.text("%s Version %s" % (info.name, info.version))
itemLoader.text("Created by %s" % info.author)
itemLoader.text("Get more help at %s" % info.url)
itemLoader.text("", comment=False) # Add a spacer between header and description
itemLoader.text("""\
// This file will be used to load all of the items. This is where you will
// enable / disable all shop items by default.

// To load an item, simply use the command:
// wcs loaditem "<item name>"
// e.g: wcs loaditem "Claws of Attack"

// To load all races within the ../addons/wcs/shopmenu/ folder use this command:
// wcs loadallitems

// Add all item loads below:
// -----------------------------------------------------------------------------
""", comment=False)

itemLoader.command("wcs")

itemLoader.text("-" * 77)

extensionConfigPath = WCS_CFG_PATH.joinpath("wcs_extension_loader.cfg")
extensionLoader = cfglib.AddonCFG(extensionConfigPath)
extensionLoader.text("%s Version %s" % (info.name, info.version))
extensionLoader.text("Created by %s" % info.author)
extensionLoader.text("Get more help at %s" % info.url)
extensionLoader.text("", comment=False) # Add a spacer between header
extensionLoader.text("""\
// This file will be used to load all of the extensions. This is where you will
// enable / disable all addons by default.

// To load an extension, simply use the command:
// wcs loadaddon "<extension name>"
// e.g: wcs loadaddon "wcs_admin"

// To load all races within the ../addons/wcs/extension_folder/ folder use this 
//   command:
// wcs loadalladdons

// Add all extension loads below:
// -----------------------------------------------------------------------------
""", comment=False)

extensionLoader.command("wcs")

extensionLoader.text("-" * 77)


def executeConfigs():
    """
    This function must execute all configurations. The reason we put these
    calls in a separate function is so we can execute the configurations after
    the commands have been registered. This enables us to execute any custom
    commands we create at load.
    """
    raceLoader.write()
    es.server.cmd("exec wcs/wcs_race_loader.cfg")
    itemLoader.write()
    es.server.cmd("exec wcs/wcs_item_loader.cfg")
    extensionLoader.write()
    es.server.cmd("exec wcs/wcs_extension_loader.cfg")
    config.write()
    es.server.cmd("exec wcs/wcs_config.cfg")